package wls;

import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class JMSSender {

	/**
	 * @param args
	 * @throws NamingException
	 * @throws JMSException
	 */
	public static void main(String[] args) throws NamingException, JMSException {
		// init JNDI context
		String JNDIFactory = "weblogic.jndi.WLInitialContextFactory";// define
																		// JNDI
																		// context
																		// factory
		String providerUrl = "t3://localhost:7001"; // define weblogic JMS url
		Hashtable<String, String> env = new Hashtable<String, String>();
		env.put(Context.INITIAL_CONTEXT_FACTORY, JNDIFactory);
		env.put(Context.PROVIDER_URL, providerUrl);
		Context ctx = new InitialContext(env);

		// find connection factory
		String connFactoryJNDI = "jms/ConnectionFactoryTest"; // jms
																// connectionFactory
																// JNDI name
		QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx
				.lookup(connFactoryJNDI);
		// create queue connection
		QueueConnection qConn = (QueueConnection) connFactory
				.createConnection();
		// create session
		QueueSession qSession = qConn.createQueueSession(false,
				Session.AUTO_ACKNOWLEDGE);
		// find queue by JNDI lookup
		Queue queue = (Queue) ctx.lookup("jms/QueueTest");
		// create sender
		QueueSender qSender = qSession.createSender(queue);
		// create message
		Message msg = qSession.createTextMessage("Message is from JMS Sender!");
		qSender.send(msg);

		qSender.close();

		qSession.close();

		qConn.close();

	}

}